package application;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.PasswordField;
import javafx.scene.control.RadioButton;

import java.net.URL;  
import java.util.ResourceBundle;  
import javafx.event.ActionEvent;  
//import javafx.fxml.FXML;  
import javafx.fxml.Initializable;  
//import javafx.scene.control.PasswordField;  
//import javafx.scene.control.TextField; 
import db.DB_tools;
import base.Person;
import base.Manager;
import base.Staff;

public class LoginController implements Initializable{
	@FXML
	private TextField loginname;
	@FXML
	private PasswordField loginpassword;
	@FXML
	private Button login;
	@FXML
	private Button cancel;
	@FXML
	private RadioButton staff;
	@FXML
	private RadioButton manager;
	
	private Main application;  
    
    
    public void setApp(Main application){  
        this.application = application;  
    }  
      
    @FXML
    public void LOGIN_M(ActionEvent event) {  
        if(staff.isSelected()||manager.isSelected()){
        int loginid = Integer.parseInt(loginname.getText());
    	int check=db.DB_tools.login(loginid, loginpassword.getText());
    	if(check==1){//if the user is correct.
        System.out.println("Success!");
        if(staff.isSelected()){//user must select one choice
        	System.out.println(staff.getText());
        	//int id=db.DB_tools.getIDFromSignUp(loginname.getText(), loginpassword.getText());
        	String name=db.DB_tools.getNamefromId(loginid, loginpassword.getText());
        	Person p = new Staff(name, loginpassword.getText(),staff.getText(),loginid);
        	//System.out.println(p.getName()+p.getID());
        	//db.DB_tools.getOneUser(loginname.getText(),id);
        	application.gotoStaffinfo(p);
    	}
        if(manager.isSelected()){//user must select one choice 
        	System.out.println(manager.getText());
        	//int id=db.DB_tools.getIDFromSignUp(loginname.getText(), loginpassword.getText());
        	String name=db.DB_tools.getNamefromId(loginid, loginpassword.getText());
        	Person p = new Manager(name, loginpassword.getText(),manager.getText(),loginid);
        	//System.out.println(p.getName()+p.getID());
        	//db.DB_tools.getOneUser(loginname.getText(),id);
        	application.gotoStaffinfo(p);
        }
    	
    	}
    	else {
    		System.out.println("Fail");
    		application.gotologin();
    	}
        }
        else {
        	System.out.println("Please choose at least one button!");
        }
    }  
    
      
    @FXML  
    private void CLEAR_M(ActionEvent event) {  
        loginname.setText(null);  
        loginpassword.setText(null);  
    }  
    
    @FXML
    private void goSignup(ActionEvent event)
    {
    	application.gotoSignup();
    }
      
    @Override  
    public void initialize(URL url, ResourceBundle rb) {  
        // TODO  
    }      

}
